package com.yidu.action;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.common.JsonMsg;
import com.yidu.common.Pages;
import com.yidu.common.Tools;
import com.yidu.domain.ListenAuditing;
import com.yidu.domain.ListenBackstageUser;
import com.yidu.domain.ListenCompany;
import com.yidu.domain.ListenPurchase;
import com.yidu.domain.ListenRawstockRecord;
import com.yidu.service.ListenAuditingService;
import com.yidu.service.ListenPurchaseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiangyu
 * @since 2018-11-27
 */
@Controller
@RequestMapping("/listenAuditing")
public class ListenAuditingController {
	@Resource
	ListenAuditingService auditingService;
	/**
	 * 添加分店采购审核
	 * @author 刘武
	 * @param listenAuditing 审核对象
	 * @return 工具类
	 */
	@RequestMapping("/insertPurchaseAudit")
	@ResponseBody
	public JsonMsg insertPurchaseAudit(ListenAuditing listenAuditing,HttpSession session){
		//取到登录的用户信息
		ListenBackstageUser users=(ListenBackstageUser) session.getAttribute("user");
		if(users!=null) {
			if(!Tools.isEmpty(users.getBuName())) {
				listenAuditing.setOper(users.getBuName());//操作人
				listenAuditing.setAuditingPeople(users.getBuName());//审核人
			}
		}
		JsonMsg jsonMsg=new JsonMsg();
		int rows=auditingService.insertPurchaseAudit(listenAuditing);
		if(rows>0) {
			jsonMsg.setDatas(1);
			jsonMsg.setMsg("增加成功");
		}else {
			jsonMsg.setDatas(2);
			jsonMsg.setMsg("增加失败");
		}
		return jsonMsg;
	}
	/**
	 * 查看采购审核详情
	 * @author 刘武
	 * @param purchase 采购订单对象
	 * @return 审核对象集合
	 */
	@RequestMapping("/selectMusicPurchaseAuditDetail")
	@ResponseBody
	public Map<String,Object> selectMusicPurchaseAuditDetail(ListenPurchase purchase){
		System.err.println("订单id："+purchase.getPurchaseId());
		System.err.println("销售id："+purchase.getSellId());
		Map<String, Object> map=new HashMap<>();
		if(!Tools.isEmpty(purchase.getPurchaseId())) {
			map.put("detailId", purchase.getPurchaseId());
		}
		if(!Tools.isEmpty(purchase.getSellId())) {
			map.put("sellId", purchase.getSellId());
		}
		
		if(purchase.getPage()!=null && purchase.getLimit()!=null) {
			Pages page=new Pages();
			page.setCurPage(purchase.getPage());
			page.setMaxResult(purchase.getLimit());
			
			map.put("firstRows", page.getFirstRows());
			map.put("maxResult", page.getMaxResult());
		}
		
		List<ListenAuditing> list = auditingService.selectMusicPurchaseAuditDetail(map);
		int rows=auditingService.selectRows(map);
		
		Map<String, Object> layuiMap=new HashMap<>();
		
		layuiMap.put("code", 0);
		layuiMap.put("msg", "");
		layuiMap.put("count",rows);
		//将list放入map中
		layuiMap.put("data", list);
		return layuiMap;
	}
	/**
	 * 查看加盟审核详情
	 * @author 刘武
	 * @param company 公司对象
	 * @return 审核对象集合
	 */
	@RequestMapping("/selectCompanyAuditDetail")
	@ResponseBody
	public Map<String,Object> selectCompanyAuditDetail(ListenCompany company){
		Map<String, Object> map=new HashMap<>();
		if(!Tools.isEmpty(company.getCompanyId())) {
			map.put("detailId", company.getCompanyId());
		}
		
		if(company.getPage()!=null && company.getLimit()!=null) {
			Pages page=new Pages();
			page.setCurPage(company.getPage());
			page.setMaxResult(company.getLimit());
			
			map.put("firstRows", page.getFirstRows());
			map.put("maxResult", page.getMaxResult());
		}
		
		List<ListenAuditing> list = auditingService.selectMusicPurchaseAuditDetail(map);
		int rows=auditingService.selectRows(map);
		
		Map<String, Object> layuiMap=new HashMap<>();
		
		layuiMap.put("code", 0);
		layuiMap.put("msg", "");
		layuiMap.put("count",rows);
		//将list放入map中
		layuiMap.put("data", list);
		return layuiMap;
	}
	
	
	/**
	 * @title 查询审核记录 
	 * @param ListenAuditing
	 * @return Map
	 * @author 江宇
	 * @dateTime 2018年12月21日
	 * @versions 1.0
	 */
	@RequestMapping("/findAuditing")
	@ResponseBody
	public Map<String,Object> findAuditing(ListenAuditing au){
		Map<String, Object> map=new HashMap<>();
		if(!Tools.isEmpty(au.getVocationalWorkId())) {
			map.put("vocationalWorkId", au.getVocationalWorkId());
		}
		
		if(au.getPage()!=null && au.getLimit()!=null) {
			Pages page=new Pages();
			page.setCurPage(au.getPage());
			page.setMaxResult(au.getLimit());
			
			map.put("firstRows", page.getFirstRows());
			map.put("maxResult", page.getMaxResult());
		}
		
		List<ListenAuditing> list = auditingService.findAuditing(map);
		List<ListenAuditing> auList=new ArrayList<>();
		for (ListenAuditing listenAuditing : list) {
			ListenAuditing au2=new ListenAuditing();
			BeanUtils.copyProperties(listenAuditing, au2);
			au2.setAuditingTimeString(Tools.SDF_TIME.format(listenAuditing.getAuditingTime()));
			auList.add(au2);
		}
		int rows=auditingService.findAuditingCount(map);
		
		Map<String, Object> layuiMap=new HashMap<>();
		
		layuiMap.put("code", 0);
		layuiMap.put("msg", "");
		layuiMap.put("count",rows);
		//将list放入map中
		layuiMap.put("data", auList);
		return layuiMap;
	}
	
	
	/** 
	 * @title 增加采购材料审核
	 * @param ListenAuditing
	 * @return int
	 * @author 江宇
	 * @dateTime 2018年12月22日
	 * @versions 1.0
	 */
	@RequestMapping("/addMaterialAuditing")
	@ResponseBody
	public int addMaterialAuditing(ListenAuditing listenAuditing,HttpSession session){
		 
		return auditingService.addMaterialAuditing(listenAuditing,session);
	}
	
	
	
	/** 
	 * @title 增加生产计划审核
	 * @param ListenAuditing
	 * @return int
	 * @author 江宇
	 * @dateTime 2018年12月22日
	 * @versions 1.0
	 */
	@RequestMapping("/addProductionAuditing")
	@ResponseBody
	public int addProductionAuditing(ListenAuditing listenAuditing,HttpSession session){
		 
		return auditingService.addProductionAuditing(listenAuditing,session);
	}
}

